package com.aerotechnic.dao.implementation;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aerotechnic.dao.interfaces.AvionDaoIn;
import com.aerotechnic.entities.Avion;
import com.aerotechnic.entities.JourFerie;

@Repository
public class AvionDao implements AvionDaoIn {
	
	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void save(Avion myEntity) {
		// TODO Auto-generated method stub
		getCurrentSession().save(myEntity);
	}

	@Override
	public void update(Avion myEntity) {
		// TODO Auto-generated method stub
		getCurrentSession().update(myEntity);
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		Avion e = findByID(id);
		if (e != null)
			getCurrentSession().delete(e);
	}

	@Override
	public Avion findByID(Long id) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		@SuppressWarnings("unchecked")
		List<Avion> avions = session.createQuery(
				"FROM Avion where id= " + id).list();
		return avions.isEmpty() ? null : avions.get(0);
	}

	@Override
	public List<Avion> findAll() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from Avion").list();
	}

	

}
